package com.xmu.prcs.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.xmu.prcs.dao.SubjectDao;
import com.xmu.prcs.entity.Subject;

/**
 * 
 * @author frequent
 * @version 1.0,2012-3-20
 *
 */
public class SubjectDaoImpl extends GenericDaoImpl<Subject> implements SubjectDao{

	public SubjectDaoImpl() {
		super(Subject.class);
	}

	public List<Subject> getSubjectsByPropety(String propety, Object param) {
		List<Subject> lists = null;
		try{
			String sql = "from Subject as model where model." + propety + " = ?";
			lists = this.getHibernateTemplate().find(sql, param);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}catch(Throwable t){
			t.printStackTrace();
			return null;
		}
		return lists;
	}

	public int countSubject() {
		//通过sql查询得到集合数
		String sql = "select count(1) from subject s";
		
		Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Query query = session.createSQLQuery(sql);
		int size;
		if(query.uniqueResult() == null){
			size = 0;
		}
		size = Integer.parseInt((query.uniqueResult().toString()));
		System.out.println("size:" + size);
		return size;
	}

	public List<Subject> getSubject4Page(int begin, int size) {
		String hql = "from Subject as model order by model.updateTime desc";
		Query query = getHibernateTemplate().getSessionFactory()
			.getCurrentSession().createQuery(hql);
		query.setFirstResult(begin);
		query.setMaxResults(size);
		
		return query.list();
	}

}
